package com.softer.chengxin.outside.service.impl;

import com.softer.chengxin.blacklist.domin.BlacklistVO;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.common.enums.ParamEnum;
import com.softer.chengxin.common.util.DateFormatUtil;
import com.softer.chengxin.outside.dao.OutsideDao;
import com.softer.chengxin.outside.domin.CorpVO;
import com.softer.chengxin.outside.domin.PageResult;
import com.softer.chengxin.outside.service.OutsideService;
import com.softer.chengxin.param.dao.ParamDao;
import com.softer.chengxin.param.entity.ParamVO;
import com.softer.chengxin.project.domin.ProjectInfoVO;
import com.softer.chengxin.scheduled.dao.ScheduledTaskDao;
import com.softer.chengxin.score.entity.ScoreDetailVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * @author wangixnrui
 * @date 2018/10/22
 */
@Service
public class OutsideServiceImpl implements OutsideService {
    @Autowired
    private OutsideDao dao;
    @Autowired
    private ParamDao paramDao;
    @Autowired
    private ScheduledTaskDao scheduledTaskDao;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    /**
     * 获取企业年度综合评价
     * @param page 页码
     * @param qyId 页码
     * @param year 年度
     * @param dj 等级
     * @return 结果
     */
    @Override
    public PageResult getCorpRecord(Page page,Integer qyId,String year,String dj,String qyName) {
        PageResult pr = new PageResult();
        int count = dao.getCorpRecordCount(qyId,year,dj,qyName);
        if(count <= NumberEnum.ZERO.getNum()) {
            return pr;
        }
        List<CorpVO> list = dao.getCorpRecord(page,qyId,year,dj,qyName);
        for(CorpVO c:list){
            int ret = dao.getBlack(c.getId());
            if(ret >= NumberEnum.ONE.getNum()){
                c.setIsBlack(NumberEnum.ONE.getNum());
            }else{
                c.setIsBlack(NumberEnum.ZERO.getNum());
            }
        }
        pr.setRows(list);
        pr.setTotal(count);
        return pr;
    }

    /**
     * 查询企业优良/负面信息列表
     * @param page 页码
     * @param infoType 信息种类
     * @param dj 等级
     * @param qyId 企业id
     * @return 结果
     */
    @Override
    public PageResult getCorpScoreDetail(Page page, Integer infoType, String dj, Integer qyId,String qyName) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        PageResult pr = new PageResult();
        //查询参数类型
        ParamVO param = getParam(infoType);
        if (param == null) {
            return pr;
        }
        int count = dao.getCorpScoreDetailCount(infoType,qyId,dj,year,qyName,param);
        if(count <= NumberEnum.ZERO.getNum()){
            return pr;
        }
        List<ScoreDetailVO> list = dao.getCorpScoreDetail(page,infoType,qyId,dj,year,qyName,param);
        for(ScoreDetailVO s:list){
            if(s.getEffectiveBeginTime()!=null) {
                s.setEffectiveBeginDate(s.getEffectiveBeginTime());
            }
            if(s.getEffectiveEndTime()!=null) {
                s.setEffectiveEndDate(s.getEffectiveEndTime());
            }
        }
        pr.setRows(list);
        pr.setTotal(count);
        return pr;
    }
    /**
     * 查询参数实体
     *
     * @param infoType 外网展示的信息类型（1优良，2负面）
     * @return
     */
    private ParamVO getParam(Integer infoType) {
        ParamVO param = null;
        if (NumberEnum.ONE.getNum().equals(infoType)) {
            //优良信息公开期限
            param = paramDao.getParma(ParamEnum.EXCELLENT_OPEN.getKey());
        } else if (NumberEnum.TWO.getNum().equals(infoType)) {
            //负面信息公开期限
            param = paramDao.getParma(ParamEnum.NEGATIVE_OPEN.getKey());
        }
        return param;
    }
    /**
     * 获取该企业下项目信息
     * @param page 页码
     * @param qyId 企业id
     * @return 结果
     */
    @Override
    public PageResult getCorpProject(Page page, Integer qyId) {
        PageResult pr = new PageResult();
        int count = dao.getCorpProjectCount(qyId);
        if(count <= NumberEnum.ZERO.getNum()){
            return pr;
        }
        List<ProjectInfoVO> list = dao.getCorpProject(page.getBeginIndex(),page.getLimit(),qyId);
        for(ProjectInfoVO p:list){
            String name = dao.getProManagerName(p.getId());
            if(StringUtils.isBlank(name)){
                name = "";
            }
            p.setManagerName(name);
            if(p.getCommitteeTime()!=null){
                p.setIsCommittee(String.valueOf(NumberEnum.ONE.getNum()));
            }else {
                p.setIsCommittee(String.valueOf(NumberEnum.ZERO.getNum()));
            }
        }
        pr.setRows(list);
        pr.setTotal(count);
        return pr;
    }

    /**
     * 通过企业名查询企业列表
     * @param page 页码
     * @param qyName 企业名
     * @return 结果
     */
    @Override
    public PageResult getCorpList(Page page, String qyName,String dj) {
        PageResult pr = new PageResult();
        int count = dao.getCorpListCount(qyName,dj);
        if(count <= NumberEnum.ZERO.getNum()){
            return pr;
        }
        List<CorpVO> list = dao.getCorpList(page,qyName,dj);
        pr.setRows(list);
        pr.setTotal(count);
        return pr;
    }

    /**
     * 获取项目详细信息
     * @param proId 项目id
     * @return 结果
     */
    @Override
    public ProjectInfoVO getProjectDetail(Integer proId) {
        ProjectInfoVO pro = dao.getProjectDetail(proId);
        if (pro != null) {
            if (pro.getCommitteeTime() != null) {
                pro.setIsCommittee(String.valueOf(NumberEnum.ONE.getNum()));
            } else {
                pro.setIsCommittee(String.valueOf(NumberEnum.ZERO.getNum()));
            }
        }
        return pro;
    }

    /**
     * 获取黑名单列表
     * @param page 页码
     * @return 结果
     */
    @Override
    public PageResult getBlackList(Page page) {
        PageResult pr = new PageResult();
        int count = dao.getBlackListCount();
        if(count <= NumberEnum.ZERO.getNum()){
            return pr;
        }
        List<BlacklistVO> list = dao.getBlackList(page.getBeginIndex(),page.getLimit());
        for(BlacklistVO b:list){
            if(b.getGmtCreate()!=null) {
                b.setGmtCreateDate(sdf.format(b.getGmtCreate()));
            }
        }
        pr.setRows(list);
        pr.setTotal(count);
        return pr;
    }

    /**
     * 查询企业基本信息
     * @param qyId 企业id
     * @return 结果
     */
    @Override
    public CorpVO getCorpBaseInfo(Integer qyId) {
        CorpVO corpVO = dao.getCorpBaseInfo(qyId);
        if(corpVO != null) {
            corpVO.setFixedPersonNum(dao.getPersonCount(qyId));
            corpVO.setCertificateNum(dao.getMnanagerCount(qyId));
        }
        return corpVO;
    }

    /**
     * 获取企业信用得分和等级
     * @param qyId 企业id
     * @return 结果
     */
    @Override
    public CorpVO getCredit(Integer qyId) {
        //获取最近设置的定时任务
        String time = scheduledTaskDao.getTime();
        //获取主管部门定义的年份
        String year = DateFormatUtil.getYear(time);
        return dao.getCredit(qyId,year);
    }




}
